
#include <unistd.h>
#include <cstdlib>
#include <cstdio>
#include <string>
using std::string;

#include "httputil.h"

#include "HTTPresponse.h"

bool HTTPResponse::parseFromfd(int sockfd)
{
    m_responseBytes.clear();
    string headLine;
    bool eof, success;
    success = HTTPUtil::readline(headLine, &eof, sockfd);
    if (!success) return false;
    success = m_headers.parseHeader(sockfd);
    if (!success) return false;
    if (!m_headers.isFieldExists("Content-Length"))
        return false;
    int length = atoi(m_headers.getFieldValue("Content-Length").c_str());
    m_success = HTTPUtil::readBytes(m_responseBytes, sockfd, length);
    return m_success;
}
